package p2p;

import java.net.InetAddress;
import java.net.MulticastSocket;
import javax.swing.UIManager;

/** Main class of the program.
 * @author Bruno
 * @author ei08150
 */
public class Main {

	/** Start point of the program.
	 * @param args the command line arguments
	 */
    public static void main(String[] args) {
        // Try to use the default Look and Feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            // Ignore
        }

		processCommandArguments(args);
		try {
			// Create multicast sockets and join multicast channels
			Global.multicastInetAddr = InetAddress.getByName(Global.multicastAddr);

			Global.controlSocket = new MulticastSocket(Global.controlPort);
			Global.controlSocket.setTimeToLive(1);
			
			Global.dataSocket = new MulticastSocket(Global.dataPort);
			Global.dataSocket.setTimeToLive(1);

			Global.controlSocket.joinGroup(Global.multicastInetAddr);
			Global.dataSocket.joinGroup(Global.multicastInetAddr);
		} catch (Exception ex) {
			System.err.println("Error initializing sockets!");
			System.exit(1);
		}

        // // Create and show the program window
        Global.frmP2P = new FrmP2P();
        Global.frmP2P.setVisible(true);

		// Launch threads
		Global.controlPortThread.start();
		Global.dataPortThread.start();

        //CLIFrontend.start();
    }

	public static void processCommandArguments(String[] args) {
		/* The order of the possible arguments are as follows:
		   -m <multicast_addr>
		   -c <control_port>
		   -d <data_port>
		   -a <algorithm used to calculate the checksums
		*/
		int currentArg = 0;

		while (currentArg < args.length) {
			if (args[currentArg].equals("-m")) {
				if(++currentArg >= args.length) {
					System.err.println("-m argument specified without value!");
					System.exit(1);
				}
				Global.multicastAddr = args[currentArg];
			}
			else if (args[currentArg].equals("-c")) {
				if(++currentArg >= args.length) {
					System.err.println("-c argument specified without value!");
					System.exit(1);
				}

				try {
					Global.controlPort = Integer.parseInt(args[currentArg]);
				}
				catch (NumberFormatException e) {
					System.err.println("-c argument's value is not a number!");
					System.exit(1);
				}

				// A port has to be greater than 0 and less than 65536
				if (Global.controlPort <= 0 || Global.controlPort >= 65536) {
					System.err.println("-c argument's value is not a valid port number!");
					System.exit(1);
				}
			}
			else if (args[currentArg].equals("-d")) {
				if(++currentArg >= args.length) {
					System.err.println("-d argument specified without value!");
					System.exit(1);
				}

				try {
					Global.dataPort = Integer.parseInt(args[currentArg]);
				}
				catch (NumberFormatException e) {
					System.err.println("-d argument's value is not a number!");
					System.exit(1);
				}

				// A port has to be greater than 0 and less than 65536
				if (Global.dataPort <= 0 || Global.dataPort >= 65536) {
					System.err.println("-d argument's value is not a valid port number!");
					System.exit(1);
				}
			}
			else if (args[currentArg].equals("-a")) {
				if(++currentArg >= args.length) {
					System.err.println("-a argument specified without value!");
					System.exit(1);
				}

				if(Global.setMessageDigestAlgorithm(args[currentArg]) == false) {
					System.err.println("-a argument's value is not a valid checksum algorithm!");
					System.exit(1);
				}
			}
			else {
				System.err.println("Unknown " + args[currentArg] + " flag found!");
			}

			currentArg++;
		}

		if(Global.controlPort == Global.dataPort) {
			System.err.println("Data and control ports cannot be the same!");
			System.exit(1);
		}
	}
}
